package com.zdkj.subway.st.util;

import lombok.experimental.UtilityClass;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Calendar;
import java.util.Date;

/**
 * @program: zdkj
 * @description:
 * @author: chen
 * @create: 2020-06-10 17:14
 **/
@UtilityClass
public class DateUtilsSt {
    /**
     * 获取当前时间yyyyMMddHHmmss格式字符串
     *
     * @return
     */
    public String getDatetimeNowFormatter() {
        return getDatetimeFormatter("yyyyMMddHHmmss");
    }

    /**
     * 获取当前时间yyyy-MM-dd格式字符串
     *
     * @return
     */
    public String getDateNowFormatter() {
        return getDatetimeFormatter("yyyy-MM-dd");
    }

    /**
     * 获取当前时间yyyy-MM格式字符串
     *
     * @return
     */
    public String getYearMonthNowFormatter() {
        return getDatetimeFormatter("yyyy-MM");
    }

    /**
     * 获取当前日期的字符串
     *
     * @return
     */
    public String getDateFormatter() {
        return getDatetimeFormatter("yyyyMMdd");
    }

    /**
     * 前N天的时间
     *
     * @param preDay
     * @return
     */
    public String getPreDayDateTime(Integer preDay) {
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return formatter.format(now.minusDays(preDay));
    }

    public String getDatetimeFormatter(String format) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
        return formatter.format(LocalDateTime.now());
    }

    /**
     * 使用LocalDateTime类下的上个月月末时间
     */
    public LocalDate lastMonthLast() {
        LocalDate now = LocalDate.now();
        LocalDate localDate = now.minusMonths(1);
        LocalDate lastTime = localDate.with(TemporalAdjusters.lastDayOfMonth());
        return lastTime;
    }

    /**
     * 使用LocalDateTime类下的 上个月月初时间
     */
    public LocalDate lastMonthFirst() {
        LocalDate now = LocalDate.now();
        LocalDate localDate = now.minusMonths(1);
        LocalDate lastTime = localDate.with(TemporalAdjusters.firstDayOfMonth());
        return lastTime;
    }

    /**
     * 日期string类型转LocalDateTime
     * @param localDateTime
     * @return
     */
    public LocalDateTime LocalDateTimeToString(String localDateTime) {
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime localDateStr = LocalDateTime.parse(localDateTime,fmt);
        return localDateStr;
    }

    /**
     * 日期string类型转LocalDateTime 自定义format
     * @param date
     * @param
     * @return
     */
    public LocalDateTime stringTolocalDateTime(String date) {
        if(date.length()<11){
            String str = date.substring(0, 10);
            date = str+" 00:00:00";
        }
        String format = "yyyy-MM-dd HH:mm:ss";
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern(format);
        LocalDateTime localDateStr = LocalDateTime.parse(date,fmt);
        return localDateStr;
    }
    /**
     * 日期string类型转LocalDate
     * @param localDate
     * @return
     */
    public LocalDate LocalDateToString(String localDate) {
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate localDateStr = LocalDate.parse(localDate,fmt);
        return localDateStr;
    }

    /**
     * 日期LocalDate类型转string
     * @param localDate
     * @return
     */
    public String StringToLocalDate(LocalDate localDate) {
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String dateStr = localDate.format(fmt);
        return dateStr;
    }

    /**
     * 日期LocalDateTime类型转string
     * @param localDateTime
     * @return
     */
    public String StringToLocalDate(LocalDateTime localDateTime) {
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String dateStr = localDateTime.format(fmt);
        return dateStr;
    }

    /**
     * 当前时间
     * @param
     * @return
     */
    public LocalDateTime newLocalDateTime() {
        String localDateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());//当前时间
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime newLocalDateTime = LocalDateTime.parse(localDateTime,fmt);
        return newLocalDateTime;
    }

    /**
     * 判断一年中的是当月第几周
     * @param datetime
     * @return
     * @throws java.text.ParseException
     */
    public static String whatWeek(String datetime){
        try {
            if(datetime.length()>12){
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date date = format.parse(datetime);
                Calendar calendar = Calendar.getInstance();
                calendar.setFirstDayOfWeek(Calendar.MONDAY);
                calendar.setTime(date);
                String weekNumbe = String.valueOf(calendar.get(Calendar.WEEK_OF_MONTH));
                return weekNumbe;
            }else {
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                Date date = format.parse(datetime);
                Calendar calendar = Calendar.getInstance();
                calendar.setFirstDayOfWeek(Calendar.MONDAY);
                calendar.setTime(date);
                String weekNumbe = String.valueOf(calendar.get(Calendar.WEEK_OF_MONTH));
                return weekNumbe;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * localDate转Timestamp
     * @param localDate
     * @return
     */
    public long LocalDateToTimestamp(LocalDate localDate) {
        long timestamp = localDate.atStartOfDay(ZoneOffset.ofHours(8)).toInstant().toEpochMilli();
        return timestamp;
    }

    /**
     * Timestamp转LocalDateTime
     * @param timestamp
     * @return
     */
    public LocalDateTime TimestampToLocalDateTime(Long timestamp) {
        LocalDateTime localDateTime = Instant.ofEpochMilli(timestamp).atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
        return localDateTime;
    }

    /**
     *  String转换成Date类型
     */
    public static Date stringToDate (String currentTime) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");//注意月份是MM
        Date date = simpleDateFormat.parse(currentTime);
        return date;
    }

    /**
     * date转LocalDateTime
     * @return
     */
    public static LocalDateTime DateTurnLocalDateTime(Date date){
        Instant instant =  date.toInstant();
        LocalDateTime ldt = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
        return ldt;
    }

    /**
     * 检验日期格式是否正确
     * @param dttm
     * @param
     * @return
     */
    public boolean isDate(String dttm,String format) {
        if (dttm == null || dttm.isEmpty() || format == null || format.isEmpty()) {
            return false;
        }

        if (format.replaceAll("'.+?'", "").indexOf("y") < 0) {
            format += "/yyyy";
            DateFormat formatter = new SimpleDateFormat("/yyyy");
            dttm += formatter.format(new Date());
        }

        DateFormat formatter = new SimpleDateFormat(format);
        formatter.setLenient(false);
        ParsePosition pos = new ParsePosition(0);
        Date date = formatter.parse(dttm, pos);

        if (date == null || pos.getErrorIndex() > 0) {
            return false;
        }
        if (pos.getIndex() != dttm.length()) {
            return false;
        }

        if (formatter.getCalendar().get(Calendar.YEAR) > 9999) {
            return false;
        }

        return true;
    }

    /**
     * 日期+1
     */
    public static String dateAddOne(String dateStr) throws ParseException {
        //对日期进行+1处理
        Date date = stringToDate(dateStr);
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        //对天进行+1，如果为月份最后一天会自动进行处理为下个月第一天
        c.add(Calendar.DAY_OF_MONTH,1);
        date = c.getTime();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String time = simpleDateFormat.format(date);
        return time;
    }
}
